package com.rjh.rocketmq.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.concurrent.TimeUnit;

public class OnewayProducer {

    public static void main(String[] args) throws Exception {
        // 实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        // 设置NameServer的地址
        //producer.setNamesrvAddr("192.168.2.107:9876;192.168.2.107:9876;192.168.2.109:9876;192.168.2.110:9876");
        producer.setNamesrvAddr("106.55.245.165:9876");
        // 启动Producer实例
        producer.start();
        for (int i = 0; i < 10; i++) {
            // 创建消息，并指定Topic，Tag和消息体
            Message msg = new Message("base",
                    "TagC" ,
                    ("Hello RocketMQ OnewayProducer " + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
            );
            // 发送消息到一个Broker
            producer.sendOneway(msg);
            System.out.println(i);
            TimeUnit.SECONDS.sleep(1);
            // 通过sendResult返回消息是否成功送达
        }
        // 如果不再发送消息，关闭Producer实例。
        producer.shutdown();
    }
}
